package com.youkeyi.ddy.cloud.basic.hr.approval.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.youkeyi.ddy.cloud.basic.hr.approval.entity.TblApprovalNode;
import com.youkeyi.ddy.cloud.basic.hr.approval.service.TblApprovalNodeService;
import com.youkeyi.ddy.cloud.common.api.basic.hr.approval.node.HrApprovalNodeCloudService;
import com.youkeyi.ddy.cloud.common.api.basic.hr.approval.node.dto.HrApprovalNodeListCloudDto;
import com.youkeyi.ddy.cloud.common.api.basic.hr.approval.node.dto.HrApprovalNodeListItemCloudDto;
import com.youkeyi.ddy.cloud.common.api.basic.hr.approval.node.param.HrApprovalNodeCreateCloudParam;
import com.youkeyi.ddy.cloud.common.api.basic.hr.approval.node.param.HrApprovalNodeDeleteCloudParam;
import com.youkeyi.ddy.cloud.common.api.basic.hr.approval.node.param.HrApprovalNodeQueryCloudParam;
import com.youkeyi.ddy.cloud.common.api.basic.hr.approval.node.param.HrApprovalNodeUpdateCloudParam;
import com.youkeyi.ddy.cloud.common.constant.YoukeyiDdyCloudBasicPublicConstant;
import com.youkeyi.ddy.cloud.common.enums.ResponseCodeEnum;
import com.youkeyi.ddy.cloud.common.response.ResponseContent;
import com.youkeyi.ddy.cloud.common.utils.SimpleUUidUtils;
import org.apache.commons.collections4.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Date;
import java.util.List;

/**
 * <p>
 * 人事审批节点表 前端控制器
 * </p>
 *
 * @author xiawang.liu
 * @since 2024-05-15
 */
@Service
public class TblApprovalNodeController implements HrApprovalNodeCloudService {

    @Autowired
    private TblApprovalNodeService tblApprovalNodeService;

    @Override
    public HrApprovalNodeListCloudDto query(HrApprovalNodeQueryCloudParam param) {
        HrApprovalNodeListCloudDto dto = new HrApprovalNodeListCloudDto();
        if (null == param) {
            dto.setErrorMessage(ResponseCodeEnum.PARAMETER_ILLEGAL.getMessage());
            return dto;
        }

        if (CollectionUtils.isNotEmpty(param.getApprovalNodePrimaryKeys())) {
            param.getApprovalNodePrimaryKeys().removeAll(Collections.singleton(null));
        }
        if (CollectionUtils.isNotEmpty(param.getCorpKeys())) {
            param.getCorpKeys().removeAll(Collections.singleton(null));
        }
        if (CollectionUtils.isNotEmpty(param.getInstanceKeys())) {
            param.getInstanceKeys().removeAll(Collections.singleton(null));
        }
        if (CollectionUtils.isNotEmpty(param.getApproverKeys())) {
            param.getApproverKeys().removeAll(Collections.singleton(null));
        }

        QueryWrapper<TblApprovalNode> wrapper = new QueryWrapper<>();
        wrapper.lambda()
                .in(CollectionUtils.isNotEmpty(param.getApprovalNodePrimaryKeys()), TblApprovalNode::getPrimaryKey, param.getApprovalNodePrimaryKeys())
                .in(CollectionUtils.isNotEmpty(param.getCorpKeys()), TblApprovalNode::getCorpKey, param.getCorpKeys())
                .in(CollectionUtils.isNotEmpty(param.getInstanceKeys()), TblApprovalNode::getInstanceKey, param.getInstanceKeys())
                .in(CollectionUtils.isNotEmpty(param.getApproverKeys()), TblApprovalNode::getApproverKey, param.getApproverKeys())
                .eq(null != param.getSequence(), TblApprovalNode::getSequence, param.getSequence())
                .ne(TblApprovalNode::getIsDeleted, YoukeyiDdyCloudBasicPublicConstant.SYSTEM_YES_OR_NO_YES.value())
        ;

        Page<TblApprovalNode> page = new Page<>();
        if (null != param.getCurrentPage() && null != param.getPageSize()) {
            page.setCurrent(param.getCurrentPage());
            page.setSize(param.getPageSize());
        } else {
            page.setCurrent(1);
            page.setSize(99999999);
        }
        Page<TblApprovalNode> nodeList = tblApprovalNodeService.page(page, wrapper);
        if (CollectionUtils.isNotEmpty(nodeList.getRecords())) {
            dto.setTotal(nodeList.getTotal());

            List<HrApprovalNodeListItemCloudDto> items = new ArrayList<>();
            nodeList.getRecords().forEach(node -> {
                HrApprovalNodeListItemCloudDto item = new HrApprovalNodeListItemCloudDto();
                item.setApprovalNodePrimaryKey(node.getPrimaryKey());
                item.setCorpKey(node.getCorpKey());
                item.setInstanceKey(node.getInstanceKey());
                item.setFormJsonStr(node.getFormJsonStr());
                item.setNodeType(node.getNodeType());
                item.setSequence(node.getSequence());
                item.setApproverKey(node.getApproverKey());
                item.setApprover(node.getApprover());
                item.setApprovalTime(node.getApprovalTime() != null ? node.getApprovalTime().getTime() : null);
                item.setApprovalResult(node.getApprovalResult());
                item.setApprovalComment(node.getApprovalComment());
                item.setSuperiorNode(node.getSuperiorNode());
                item.setSubordinateNode(node.getSubordinateNode());
                item.setRemarks(node.getRemarks());
                item.setIsDeleted(node.getIsDeleted());
                item.setIsDeletedName(YoukeyiDdyCloudBasicPublicConstant.SYSTEM_YES_OR_NO.getName(item.getIsDeleted()));
                item.setCreateTime(node.getCreateTime() != null ? node.getCreateTime().getTime() : null);
                item.setCreateUserKey(node.getCreateUserKey());
                item.setCreateUserName(node.getCreateUserName());
                item.setUpdateTime(node.getUpdateTime() != null ? node.getUpdateTime().getTime() : null);
                item.setUpdateUserKey(node.getUpdateUserKey());
                item.setUpdateUserName(node.getUpdateUserName());
                items.add(item);
            });
            dto.setRows(items);
        }
        dto.setSuccessMessage(ResponseCodeEnum.SUCCESS.getMessage());
        return dto;
    }

    @Override
    public ResponseContent create(HrApprovalNodeCreateCloudParam param) {
        ResponseContent rc = new ResponseContent();
        if (null == param || StringUtils.isBlank(param.getInstanceKey())) {
            rc.setErrorMessage(ResponseCodeEnum.PARAMETER_ILLEGAL.getMessage());
            return rc;
        }
        TblApprovalNode node = new TblApprovalNode();
        node.setPrimaryKey(SimpleUUidUtils.getUuid());
        node.setCorpKey(param.getCorpKey());
        node.setInstanceKey(param.getInstanceKey());
        node.setFormJsonStr(param.getFormJsonStr());
        node.setNodeType(param.getNodeType());
        node.setSequence(param.getSequence());
        node.setApproverKey(param.getApproverKey());
        node.setApprover(param.getApprover());
        node.setApprovalTime(param.getApprovalTime() != null ? new Date(param.getApprovalTime()) : null);
        node.setApprovalResult(param.getApprovalResult());
        node.setApprovalComment(param.getApprovalComment());
        node.setSuperiorNode(param.getSuperiorNode());
        node.setSubordinateNode(param.getSubordinateNode());
        node.setRemarks(param.getRemarks());
        node.setIsDeleted(YoukeyiDdyCloudBasicPublicConstant.SYSTEM_YES_OR_NO_NO.value());
        node.setCreateUserKey(param.getCreateUserKey());
        node.setCreateUserName(param.getCreateUserName());
        node.setCreateTime(new Date());
        boolean save = tblApprovalNodeService.save(node);
        if (!save) {
            rc.setErrorMessage(ResponseCodeEnum.FAIL.getMessage());
            return rc;
        }
        rc.setSuccessMessage(ResponseCodeEnum.SUCCESS.getMessage());
        rc.setKey(node.getPrimaryKey());
        return rc;
    }

    @Override
    public ResponseContent update(HrApprovalNodeUpdateCloudParam param) {
        ResponseContent rc = new ResponseContent();
        if (null == param || StringUtils.isBlank(param.getApprovalNodePrimaryKey())) {
            rc.setErrorMessage(ResponseCodeEnum.PARAMETER_ILLEGAL.getMessage());
            return rc;
        }

        UpdateWrapper<TblApprovalNode> wrapper = new UpdateWrapper<>();
        wrapper.lambda()
                .eq(TblApprovalNode::getPrimaryKey, param.getApprovalNodePrimaryKey());
        TblApprovalNode node = new TblApprovalNode();
        node.setPrimaryKey(param.getApprovalNodePrimaryKey());
        node.setFormJsonStr(param.getFormJsonStr());
        node.setNodeType(param.getNodeType());
        node.setSequence(param.getSequence());
        node.setApproverKey(param.getApproverKey());
        node.setApprover(param.getApprover());
        node.setApprovalTime(param.getApprovalTime() != null ? new Date(param.getApprovalTime()) : null);
        node.setApprovalResult(param.getApprovalResult());
        node.setApprovalComment(param.getApprovalComment());
        node.setSuperiorNode(param.getSuperiorNode());
        node.setSubordinateNode(param.getSubordinateNode());
        node.setRemarks(param.getRemarks());
        node.setUpdateUserKey(param.getUpdateUserKey());
        node.setUpdateUserName(param.getUpdateUserName());
        node.setUpdateTime(new Date());
        boolean save = tblApprovalNodeService.update(node, wrapper);
        rc.setSuccessMessage(ResponseCodeEnum.SUCCESS.getMessage());
        if (!save) {
            rc.setErrorMessage(ResponseCodeEnum.FAIL.getMessage());
            return rc;
        }
        return rc;
    }

    @Override
    public ResponseContent delete(HrApprovalNodeDeleteCloudParam param) {
        ResponseContent rc = new ResponseContent();
        if (null == param || CollectionUtils.isEmpty(param.getApprovalNodePrimaryKeys())) {
            rc.setErrorMessage(ResponseCodeEnum.PARAMETER_ILLEGAL.getMessage());
            return rc;
        }
        UpdateWrapper<TblApprovalNode> wrapper = new UpdateWrapper<>();
        wrapper.lambda()
                .in(TblApprovalNode::getPrimaryKey, param.getApprovalNodePrimaryKeys());

        TblApprovalNode node = new TblApprovalNode();
        node.setIsDeleted(YoukeyiDdyCloudBasicPublicConstant.SYSTEM_YES_OR_NO_YES.value());
        node.setUpdateUserKey(param.getUpdateUserKey());
        node.setUpdateUserName(param.getUpdateUserName());
        node.setUpdateTime(new Date());
        boolean save = tblApprovalNodeService.update(node, wrapper);
        rc.setSuccessMessage(ResponseCodeEnum.SUCCESS.getMessage());
        if (!save) {
            rc.setErrorMessage(ResponseCodeEnum.FAIL.getMessage());
        }
        return rc;
    }
}
